package edu.hubu.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import net.moon.jpa.model.BaseEntity;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author moonlan
 * @date 2020/10/18 下午12:49
 */
@Entity
@Table(name = "credit")
//逻辑删除注解，删除sql变成了update
@SQLDelete(sql = "update credit set credit_credit_is_deleted = true where credit_id = ?")
//where条件带上了逻辑删除条件
@Where(clause = "credit_credit_is_deleted = false")
@ApiModel("学分")
@JsonIgnoreProperties(value={"hibernateLazyInitializer", "handler", "fieldHandler"})
public class Credit extends BaseEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "snowflakeId")
    @GenericGenerator(name = "snowflakeId", strategy = "edu.hubu.commons.SnowflakeId",
                      parameters = {@org.hibernate.annotations.Parameter(name = "prefix", value = "CREDIT")})
    @Column(name = "credit_id")
    @ApiModelProperty(value = "credit的id", position = 1)
    private String creditId;

    @Column(name = "credit_credit")
    @ApiModelProperty(value = "学分", position = 2)
    private Double credit;

    @Column(name = "credit_credit_is_forbidden")
    @ApiModelProperty(value = "学分是否被禁用", position = 3)
    private boolean creditIsForbidden = false;

    @Column(name = "credit_credit_is_deleted")
    @ApiModelProperty(value = "学分是否被删除", position = 4)
    private boolean creditIsDeleted = false;

    @OneToMany(mappedBy = "credit", fetch = FetchType.LAZY)
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private List<TeacherCourse> teacherCourses = new ArrayList<>();

    @OneToMany(mappedBy = "credit", fetch = FetchType.LAZY)
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private List<StudentCourse> studentCourses = new ArrayList<>();

    public Credit(String creditId, Double credit, boolean creditIsForbidden, boolean creditIsDeleted, List<TeacherCourse> teacherCourses, List<StudentCourse> studentCourses) {
        this.creditId = creditId;
        this.credit = credit;
        this.creditIsForbidden = creditIsForbidden;
        this.creditIsDeleted = creditIsDeleted;
        this.teacherCourses = teacherCourses;
        this.studentCourses = studentCourses;
    }

    public Credit() {
    }

    public static CreditBuilder builder() {
        return new CreditBuilder();
    }

    public String getCreditId() {
        return this.creditId;
    }

    public Double getCredit() {
        return this.credit;
    }

    public boolean isCreditIsForbidden() {
        return this.creditIsForbidden;
    }

    public boolean isCreditIsDeleted() {
        return this.creditIsDeleted;
    }

    public List<TeacherCourse> getTeacherCourses() {
        return this.teacherCourses;
    }

    public List<StudentCourse> getStudentCourses() {
        return this.studentCourses;
    }

    public void setCreditId(String creditId) {
        this.creditId = creditId;
    }

    public void setCredit(Double credit) {
        this.credit = credit;
    }

    public void setCreditIsForbidden(boolean creditIsForbidden) {
        this.creditIsForbidden = creditIsForbidden;
    }

    public void setCreditIsDeleted(boolean creditIsDeleted) {
        this.creditIsDeleted = creditIsDeleted;
    }

    public void setTeacherCourses(List<TeacherCourse> teacherCourses) {
        this.teacherCourses = teacherCourses;
    }

    public void setStudentCourses(List<StudentCourse> studentCourses) {
        this.studentCourses = studentCourses;
    }

    public static class CreditBuilder {
        private String creditId;
        private Double credit;
        private boolean creditIsForbidden;
        private boolean creditIsDeleted;
        private List<TeacherCourse> teacherCourses;
        private List<StudentCourse> studentCourses;

        CreditBuilder() {
        }

        public CreditBuilder creditId(String creditId) {
            this.creditId = creditId;
            return this;
        }

        public CreditBuilder credit(Double credit) {
            this.credit = credit;
            return this;
        }

        public CreditBuilder creditIsForbidden(boolean creditIsForbidden) {
            this.creditIsForbidden = creditIsForbidden;
            return this;
        }

        public CreditBuilder creditIsDeleted(boolean creditIsDeleted) {
            this.creditIsDeleted = creditIsDeleted;
            return this;
        }

        public CreditBuilder teacherCourses(List<TeacherCourse> teacherCourses) {
            this.teacherCourses = teacherCourses;
            return this;
        }

        public CreditBuilder studentCourses(List<StudentCourse> studentCourses) {
            this.studentCourses = studentCourses;
            return this;
        }

        public Credit build() {
            return new Credit(creditId, credit, creditIsForbidden, creditIsDeleted, teacherCourses, studentCourses);
        }

        public String toString() {
            return "Credit.CreditBuilder(creditId=" + this.creditId + ", credit=" + this.credit + ", creditIsForbidden=" + this.creditIsForbidden + ", creditIsDeleted=" + this.creditIsDeleted + ", teacherCourses=" + this.teacherCourses + ", studentCourses=" + this.studentCourses + ")";
        }
    }
}
